home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / MacStarter Pascal 1.0 / Text Window starter / textPrintProcs.p < prev   
Encoding:
Text File  |  1993-12-11  |  14.6 KB  |  354 lines  |  [TEXT/PJMM]

  1.  
  2. { THIS IS AN EXAMPLE OF A PROGRAM WRITTEN USING A SUBCLASS OF xWindow TO }
  3. { IMPLEMENT A SIMPLE TEXT EDITOR PROGRAM.  NOTE THAT THE SUBCLASS }
  4. { xFileTextWindow CONTAINS FILE-MANIPULATION PROCEDURES, AND THAT }
  5. { COMMANDS HAVE BEEN ADDED TO THE FILE MENU THAT USE THESE PROCEDURES.  }
  6. { THE RESOURCE FILE ALSO CONTAINS OTHER RESOURCES REQUIRED IN ADDITION }
  7. { TO THE USUSAL ONES USED BY xWindows AND StandardMain. }
  8.  
  9. { This file is a modified version of the file applicationProcs.p.   Most of the comments from }
  10. { the original file are still here. }
  11.  
  12. { This sample program requires modified resources in the file TextPrint.rsrc, rather }
  13. { than the resource file generic.rsrc used by the project generic.π. }
  14.  
  15. { This unit would be a natural starting point for a program that uses small text files }
  16. { and also uses windows of other types.  You could simply declare and use other window }
  17. { types as appopriate. }
  18.  
  19.  
  20. unit applicationProcs;
  21.  
  22. interface
  23.  
  24. uses
  25.     xWindow, xTextWindow, xFileTextWindow, PrintTraps, TextPrint;
  26.                   { I have omitted xControlDecoration and xWindowDecoration, which are }
  27.                   { not used in this sample program.  And I have added several other UNITs }
  28.                   { that are used. }
  29.                   {    To use any of the other units that define subclasses of xWindow and }
  30.                   { of xWindowDecoration, you must add them here.  (If one of the units uses }
  31.                   { definitions from other units, the one that USES the definitions must follow }
  32.                   { the one that contains the definition.) }
  33.  
  34. const
  35.  
  36.     maxSleepTime = 5;   { When the main program has no events to process, it "goes to }
  37.           { sleep" and yields processing time to other processes that may be running.  }
  38.           { However, this program still wants to process "idle" events.  The constant }
  39.           { maxSleepTime specifies the longest time this program is willing to sleep }
  40.           { between idle events.  The time is specified in ticks (60 ticks = 1 second), so }
  41.           { a value of 5 corresponds to about 12 idle events per second, which is OK for }
  42.           { most purposes.  However, if you are using idle events to drive a processing }
  43.           { task (such as drawing successive pieces of a graph on each idle event), you }
  44.           { can change this value, setting it as low as 0 if you like, in which case the }
  45.           { Mac will give as little time as possible to other processes. }
  46.  
  47.  
  48.   { The constants defined below are used by the main program to set up the "About }
  49.   { Box" that is displayed when the user chooses "About . . .  " from the apple menu. }
  50.   { Change them as appropriate for the program you are writing. }
  51.  
  52.     ApplicationShortName = 'Simple Edit';
  53.          { This is the name of the program displayed in the first line of the Apple }
  54.          { Menu.  In this example, "About Simple Edit..." will appear there. }
  55.  
  56.     ApplicationLongName = 'A Simple Text Editor program';
  57.         { Provides a fuller description of the program that will be displayed at the }
  58.         { top of the "About Box". }
  59.  
  60.     AuthorName = 'David Eck';
  61.     AuthorAddress1 = 'Hobart and William Smith Colleges';
  62.     AuthorAddress2 = 'Geneva, NY   14456';
  63.     AuthorAddress3 = '(Email Address:   eck@hws.BITNET)';
  64.        { These four items are displayed on the next four lines of the About Box, under }
  65.        { the heading "Created By:"  You can, of course, leave some of these empty, or }
  66.        { use some of them for information other than an address. }
  67.  
  68.     CommentLine = 'This demonstration text editing program can edit only short text files.';
  69.        { This line is simply displayed at the bottom of the about box. }
  70.  
  71. var
  72.     editMenu: MenuHandle;   { These two variables provide references to the File and }
  73.     fileMenu: MenuHandle;    { Edit menus.  The main program requires that they be here. }
  74.  
  75.  
  76. { The following list of procedures is called by the main program.  You should not}
  77. { remove or change the name or parameter list for any of these procedures.  If your }
  78. { program does not require one of these procedures, just leave its definition blank. }
  79.  
  80. procedure InitializeApplication;
  81.       { Called when the program is first starting up; use this procedure to initialize }
  82.       { any global variables, open any windows that you want to appear automatically }
  83.       { on the screen at startup, etc.  }
  84.  
  85. procedure CleanUpApplication;
  86.       { This is called just before the program ends, to give you a final chance to clean }
  87.       { up.  Most programs will have no use for this.  There is no way to abort program }
  88.       { termination from this procedure. }
  89.  
  90. procedure DoEditMenu (itemNum: integer);
  91.       { The user has chosen the specified item number from the Edit menu.  This }
  92.       { procedure should carry out that command.  (Items in menus are numbered }
  93.       { from the top down, starting with 1.  Separating lines in the menu are numbered}
  94.       { even though they are not really commands.) }
  95.  
  96. procedure DoFileMenu (itemNum: integer;
  97.                             var done: boolean);
  98.       { Perform command number "itemNum" from the File Menu; if the user has chosen }
  99.       { the Quit command, then the parameter "done" should be set to TRUE, unless }
  100.       { the command is canceled in one way or another (for example, if you put up a }
  101.       { dialog box that says "Do you really want to quit?" and the user answers No). }
  102.       {    In this sample program, the comands are New, Open, Save, Save As, Close, }
  103.       { and Quit, with  dividing line between Close and Quit. }
  104.  
  105. procedure DoOtherMenu (menuNum, itemNum: integer);
  106.       { This procedure is here just in case you add more menus to the menu bar.  The }
  107.       { user has selected item number "itemNum" from menu number "menuNum", }
  108.       { and this procedure should carry out that command.   Note that the menu number }
  109.       { used here is the menu ID (or resource number), not the position of the menu in }
  110.       { the menu bar. }
  111.  
  112. procedure UpdateMenus;
  113.       { This procedure is called just BEFORE the user makes a selection from the }
  114.       { menu bar, after the user has clicked in the menu bar, but before any menu is }
  115.       { displayed.  Use this procedure to enable and disable items in the menu, to set }
  116.       { the names of items with changeable names, to check and uncheck items, etc. }
  117.  
  118. procedure ApplicationIdle;
  119.       { This procedure is called periodically--at least six times a second, unless the }
  120.       { computer is otherwise occupied (for example, tied up in a lengthy computation). }
  121.       { Note that for text windows, this procedure is used to blink the insertion point }
  122.       { in the active window.  This is done when an idle message is sent to the front }
  123.       { window. }
  124.  
  125. implementation
  126.  
  127.  
  128. {------------------Outline of defintion of new window class.----------------}
  129.  
  130. { NOTE:  The class myWin is not used in this sample program.  However, its definition is }
  131. { included here as a basis for including other windows in programs you might write }
  132. { based on this one.  (This is just a copy of the stuff from my standard applicationProcs.p }
  133.  
  134.  
  135. type
  136.  
  137. { To write your own application, you will probably declare one or more new window }
  138. { classes as subclasses of xWindow or of one of its existing subclasses. }
  139. { Here, a class myWin is defined which overrides several of the procedures from the }
  140. { parent class.  These procedures are the ones that would most commonly have to }
  141. { be overriden to create a new class.  Other procedures that you might commonly want }
  142. { to override include:  idle, doClose, doHScroll, doVScroll.  See the defintion of the }
  143. { class xWindow and its sub-classes for more information. }
  144. {    In a more realistic example, you would very probably have one or more instance }
  145. { variables in your window class to hold any data pertinent to a particular window, }
  146. { (for example, an array representing the contents of a checkers board). }
  147.  
  148.     myWin = object(xWindow)
  149.             procedure setDefaults;
  150.             override;
  151.             procedure openInRect (title: string;
  152.                                         left, top, right, bottom: integer);
  153.             override;
  154.             procedure doRedraw (badRect: Rect);
  155.             override;
  156.             procedure doContentClick (localPt: Point;
  157.                                         modifiers: longint);
  158.             override;
  159.             procedure doKey (ch: char;
  160.                                         modifiers: longint);
  161.             override;
  162.         end;
  163.  
  164. { Next, give the definitions of these overriden procedures. }
  165.  
  166. procedure myWin.setDefaults;
  167. { This procedure sets up the defalut appearance of the window, and initializes certain }
  168. { instance variables.  It is called BEFORE the window is opened, by openInRect. }
  169.     begin
  170.         inherited setDefaults;
  171.     end;
  172.  
  173. procedure myWin.openInRect (title: string;
  174.                                 left, top, right, bottom: integer);
  175. { The purpose of this procedure is to open a window, initialize its data structures, etc. }
  176. { The title is displayed in the title bar of the window; the position and size of the }
  177. { window are determined by the remaining parameters.  (top,left) is the point at the }
  178. { top, left corner of the inside part of the window, and (right,bottom) is the bottom }
  179. { right corner.   Ordinarily, this procedure is called by procedure Open, instead of }
  180. { being used directly. }
  181.     begin
  182.         inherited openInRect(title, left, top, right, bottom);
  183.     end;
  184.  
  185.  
  186. procedure myWin.doRedraw (badRect: Rect);
  187. { This procedure is called when all or part of the stuff in the window needs to be }
  188. { redrawn.  The parameter badRect specifies the region of the window that needs to }
  189. { be redrawn; the usual thing to do is to just ignore it and to redraw the entire }
  190. { window. }
  191. {     This procedure is called, for example, when another window that has been }
  192. { covering this one is moved or closed.  By default, it is also called when the user }
  193. { changes the position of the scroll bar.  In both cases, the widow is erased before }
  194. { the procedure is called.   (Erasing and redrawing the window may not be the effect }
  195. { you want when the window is scrolled; to change this default behaviour, you have }
  196. { to override the procedures doHScroll and doVScroll.) }
  197. {   The default method in xWindow for doRedraw redraws any }
  198. {  xWindowDecorations in the Window.  Some subclasses of xWindow have }
  199. { other responses to the message doContentClick.  Unless you are sure you }
  200. { have no reason to do so, you sould call INHERITED doContentClick in addition }
  201. { to any other redrawing you do for your window. }
  202.     begin
  203.         inherited doRedraw(badRect);  { ordinarily, you should call this somewhere in your procedure  }
  204.     end;
  205.  
  206.  
  207. procedure myWin.doContentClick (localPt: Point;
  208.                                 modifiers: longint);
  209.   { This procedure is called when the user clicks on the "content region" of the }
  210.   { window.  (If the user clicks on the scroll bar, title bar, or grow box, that is }
  211.   { handled automatically; this procedure is not called in such cases.)   The point }
  212.   { where the user clicked is given by the parameter localPt.  (The point is in "local }
  213.   { coordinates," i.e. is specified relative to the top left corner of the window.) }
  214.   { The parameter modifiers can be used to check whether the shift, command or }
  215.   { option keys were held down when the mouse was clicked.  This sample program }
  216.   { does not respond to clicks. }
  217.   {   The default method in xWindow for doContentClick sends the doClick to any appropriate }
  218.   {  xWindowDecoration in the Window.  Some subclasses of xWindow have }
  219.   { other responses to the message doContentClick.  Unless you are sure you }
  220.   { have no decorations in your window, you sould call INHERITED doContentClick }
  221.   { whenever your procedure does not handle the click itself. }
  222.     begin
  223.         inherited doContentClick(localPt, modifiers);
  224.     end;
  225.  
  226.  
  227. procedure myWin.doKey (ch: char;
  228.                                 modifiers: longint);
  229.   { This procedure is called when the user presses a key, provided this is the front }
  230.   { (or "active") window.  This procedure is not used by the sample program. }
  231.   {  The default method in xWindow for doKey sends a doKey message to any appropriate }
  232.   {  xWindowDecoration in the Window.  Some subclasses of xWindow have }
  233.   { other responses to the message doKey.  Unless you want some other response }
  234.   { to a key, you sould call INHERITED doKey .  }
  235.     begin
  236.         inherited doKey(ch, modifiers)
  237.     end;
  238.  
  239.  
  240. procedure OpenAWindow;
  241. { A very common procedure is one for opening a new window; not used in this sample program! }
  242.     var
  243.         Win: myWin;
  244.     begin
  245.         new(Win);
  246.         Win.open('Sample Window');
  247.     end;
  248.  
  249.  
  250.  
  251.  
  252. {------------- Definitions of procedures called by the main program, or ------------}
  253. {-----------------------used in the sample edit program---------------------}
  254.  
  255.  
  256. procedure InitializeApplication;
  257. { initialize unit xTextFileWindow, and open an initial editting window. }
  258.     begin
  259.         InitTextFileWindows;
  260.         PrintDataRecord := nil;  { from unit TextPrint }
  261.         doNewCommand;   { from unit xTextFileWindow }
  262.     end;
  263.  
  264. procedure CleanUpApplication;
  265. { no clean up necessary in this program }
  266.     begin
  267.     end;
  268.  
  269. procedure ApplicationIdle;
  270. { send idle message to front window, it there is one and if it is an xWindow }
  271.     var
  272.         win: WindowPtr;
  273.         X: xWindow;
  274.     begin
  275.         win := FrontWindow;
  276.         if (win <> nil) & (Window2xWindow(win, X)) then
  277.             X.idle;   { blinks cursor in text window }
  278.     end;
  279.  
  280. procedure UpdateMenus;
  281.     var
  282.         win: WindowPtr;
  283.         X: xWindow;
  284. { enable and disable menu items, as appropriate }
  285.     begin
  286.         twUpdateEditMenu(editMenu);  { from UNIT xTextWindow }
  287.         UpdateFileMenu(fileMenu);  { from UNIT xFileTextWindow }
  288.         win := FrontWindow;
  289.         if (win <> nil) & (Window2xWindow(win, X)) & member(x, xTextWindow) then
  290.             EnableItem(fileMenu, 7)
  291.         else
  292.             DisableItem(FileMenu, 7);
  293.     end;
  294.  
  295. procedure doPrintCommand;
  296.     var
  297.         win: WindowPtr;
  298.         X: xWindow;
  299.         theText: CharsHandle;
  300.         str: string;
  301.     begin
  302.         win := FrontWindow;
  303.         if (win <> nil) & (Window2XWindow(win, X)) & (member(X, xTextWindow)) then begin
  304.                 theText := xTextWindow(X).GetText;
  305.                 str := X.GetTitle;
  306.                 str := StringOf(str, ', \d, \t\r Page \p');
  307.                 PrintText(theText, GetHandleSize(Handle(theText)), str, geneva, 12, 0);
  308.             end;
  309.     end;
  310.  
  311. procedure DoFileMenu (itemNum: integer;
  312.                                 var done: boolean);
  313.     begin  { commands are sent to procedures in unit xTextFileWindow }
  314.         case itemNum of
  315.             1: 
  316.                 doNewCommand;
  317.             2: 
  318.                 doOpenCommand;
  319.             3: 
  320.                 doSaveCommand;
  321.             4: 
  322.                 doSaveAsCommand;
  323.             5: 
  324.                 doCloseCommand;
  325.             7: 
  326.                 doPrintCommand;
  327.             8: 
  328.                 doPageSetup;
  329.             10: 
  330.                 CloseAllBeforeQuitting(done);
  331.         end;
  332.     end;
  333.  
  334. procedure DoEditMenu (itemNum: integer);
  335.  { command is sent to the front window, if it is an xTextWindow (as it has to be }
  336.  { in this sample program, if there is any active window at all) }
  337.     var
  338.         win: windowPtr;
  339.         xWin: xWindow;
  340.     begin
  341.         win := FrontWindow;
  342.         if win <> nil then begin
  343.                 xWin := xWindow(WindowPeek(win)^.refCon);
  344.                 if (xWin <> nil) & member(xWin, xTextWindow) then
  345.                     xTextWindow(xWin).doEditMenu(itemNum);
  346.             end;
  347.     end;
  348.  
  349. procedure DoOtherMenu (menuNum, itemNum: integer);
  350. { no other menus are used in this program }
  351.     begin
  352.     end;
  353.  
  354. end.